"""UI 操作页面"""

from bll import *
from model import *


class InputEroor(Exception):
    """自定义异常"""

    def __init__(self, message, content, num_line, serial):
        self.message = message
        self.content = content
        self.num_line = num_line
        self.serial = serial


class StudentManagerView:
    """
    显示类，视图模块
    """

    def __init__(self):
        self.__kz = StudentManagerController()

    def __display_menu(self):
        """
        显示菜单
        """
        print("1) 添加学生")
        print("2) 显示学生")
        print("3) 删除学生")
        print("4) 修改学生")
        print("5) 按照成绩升序排列学生")
        print("6) 退出系统")

    def __select_menu(self):
        item = input("请输入序号：")
        if item == "1":
            self.__input_students()  # 添加
        elif item == "2":
            self.__output_students()  # 显示
        elif item == "3":
            self.__re_students()  # 删除
        elif item == "4":
            self.__modify_students()  # 修改
        elif item == "5":
            self.__output_students_up()  # 升序
        else:
            raise InputEroor("输入正确的序号！", f"该{item}内容，无法识别!", f"错误在第{34}行", "编码为：100")

    def __input_students(self):
        name = input("输入姓名：")
        age = self.__input_content("输入年龄：")
        # score = input("输入成绩：")
        score = self.__input_content("输入成绩：")
        sex = input("输入性别：")
        self.menry = StudentModel(name, age, score, sex)  # 实例化一个学生
        self.__kz.add_student(self.menry)  # 添加进列表

    def __output_students(self):
        for i in self.__kz.stu_list:
            print(i.id, i.name, i.age, i.score, i.sex)

    def __re_students(self):
        self.__output_students()  # 显示，选择
        i = int(input("选择那个学生id进行删除："))
        if self.__kz.re_student(i):
            print("删除成功！")
        else:
            raise InputEroor("删除失败!", f"该({i})数字无法识别!", "错误在62行", "编号为：101(__re_students())")

    def __modify_students(self):
        self.__output_students()  # 显示
        print("选择编号，修改个人信息：")
        name = input("输入姓名：")
        age = self.__input_content("输入年龄：")
        score = self.__input_content("输入成绩：")
        sex = input("输入性别：")
        id = self.__input_content("输入选择的编号：")
        self.ls = StudentModel(name, age, score, sex, id)
        self.__kz.update_student(self.ls)

    def __input_content(self, message):
        while True:
            try:
                id = int(input(message))
                return id
            except ValueError as e:
                print(f"输入有误，需要相关的{e}！")

    def __output_students_up(self):
        self.__kz.stuIDsort()
        self.__output_students()  # 显示

    def main(self):
        while True:
            try:
                self.__display_menu()
                self.__select_menu()
            except InputEroor as e:
                print(e)
